﻿class com.go4flash.ScrollBar extends MovieClip
{
	public var enabled;
	public var scr_pos;
	public var McHeight;
	public var PageSize;
	public var FullBarMc;
	public var McScaledStep;
	public var step;
	public var ArrowDown;
	public var height;
	public var Chunks;
	
	public function ScrollBar()
	{ 
		this.enabled	= true;
		this.scr_pos	= 0;
		
		this.createInterface();
		this.init();
	}
	
	public function init()
	{
			if(this.McHeight > this.PageSize)
				this.enabled = true;
			else 
			{
				this.FullBarMc.Slider._visible = false;
				this.enabled = false;
			}
	
			if(this.enabled)
			{
				var trg_coef		= this.PageSize/this.McHeight;
				this.McScaledStep	= (this.McHeight-this.PageSize)/(this.Chunks-1);
				var sh				= this.FullBarMc.Bar._height*trg_coef;
		
				if(sh < 10)			this.FullBarMc.Slider.SliderMc._height = 10;
				else 				this.FullBarMc.Slider.SliderMc._height = sh;
		
				var scr_coef	= this.FullBarMc.Slider._height/this.FullBarMc.Bar._height;
				var trg_h		= (this.McHeight-this.PageSize);
				var scr_h		= (this.FullBarMc.Bar._height-this.FullBarMc.Slider._height);
	
				this.step		= scr_h/(this.Chunks-1);
			
				if(!this.FullBarMc.Slider._visible)
					this.FullBarMc.Slider._visible = true;				
			}
			
			this.Update();
	}
	
	public function onMoveSlider()
	{
	  if(this.enabled)
		{
		 this.scr_pos = this.setRanges(Math.round((this.FullBarMc.Bar._ymouse-this.FullBarMc.Slider.yOffset)/this.step),0,this.Chunks-1);
		 this.Update();
		}
	}
	
	public function getScaledStep()
	{
		return this.McScaledStep;
	}
	
	public function Update()
	{
			this.scr_pos    			= this.getScrollPos();
			this.FullBarMc.Slider._y	= this.scr_pos*this.step;
			this._parent.onScroll();
	}
	
	public function getScrollPos(){
			return this.setRanges(this.scr_pos,0,this.Chunks-1);
	}
	
	public function setScrollPos(nPos){
			this.scr_pos = this.setRanges(nPos,0,this.Chunks-1);
			this.Update();
	}
	
	public function setRanges(x,l,r)
	{
			if(x <= l)	return l;
			if(x >= r)	return r;	
						return x;
	}
	
	public function setHeight(sz){
		if(sz > 25)
		{
		 this.FullBarMc.Bar.BarBgrMc._height	= sz-this.ArrowDown._height*2;
		 this.ArrowDown._y						= sz;//-this.ArrowDown._height;
		 this.init();
		}
	}
	
	public function setEnabled(boolVal)
	{
		this.enabled = boolVal;
	}
	
	public function createInterface()
	{
		var h = this._height;
		
		this._xscale = 100;
		this._yscale = 100;
	
		if(this.height!=undefined)	this.setHeight(this.height);
		else						this.setHeight(h);
	}

}
